package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.salesmanage.util.OrganizeCustom;
import com.turing.post.salesmanage.util.OrganizeCustomDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.salesmanage.util.RedempOrder;
import com.turing.post.salesmanage.util.RedempOrderDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 同一实名制客户，累计赎回西邮寄卡金额超过5000元的
 * 
 * @author wzd
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule9")
public class AlarmRule9 implements AlarmRulesBusiness {
	
	@PersistenceContext
	protected EntityManager _em;

	// 赎回单
	@EJB(mappedName = "salesmanage.impl.RedempOrderDaoImpl")
	private RedempOrderDao redempOrderDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao personalCustomDaoImpl;
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;
	@EJB(mappedName = "salesmanage.impl.OrganizeCustomDaoImpl")
	private OrganizeCustomDao organizeCustomDaoImpl;

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {
		String ruleParameter = "";// 规则
		int ruleday = 0;// 规则天数
		double rulemoney = 0;// 规则金额
		try {
			if (!PCPSUtil.isEmpty(issueCompanyNo) || tranDate != null
					|| alarmRule != null) {// 得到规则参数
				ruleParameter = alarmRule.getRuleParameter();
			}
			if (!PCPSUtil.isEmpty(ruleParameter)) {// 给定义的参数赋值
				String[] parameter = ruleParameter.split(":");
				if (parameter.length > 0) {
					ruleday = parameter[0] == null ? 0 : Integer
							.parseInt(parameter[0]);
					rulemoney = parameter[1] == null ? 0 : Double
							.parseDouble(parameter[1]);
				}
			}
			Date starttranDate = stringTodate(getSubductionDays(tranDate,
					ruleday));// 得到起始日期
			List<Object[]> list = redempOrderDaoImpl.getRedempOrderByAlarm(
					issueCompanyNo, starttranDate, tranDate, rulemoney);// 检索预警数据
			Object[] redemporder = null;
			String onwerid = "";
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					redemporder = list.get(i);
					if (redemporder != null) {
						if (redemporder[1] != null) {
							onwerid = redemporder[1].toString();
						} else if (redemporder[2] != null) {
							onwerid = redemporder[2].toString();
						}
						if (!PCPSUtil.isEmpty(onwerid)) {
							List<RedempOrder> datilelist = redempOrderDaoImpl
									.getRedempOrder(issueCompanyNo,
											starttranDate, tranDate, onwerid);// 得到风险用户赎回单的详细							
							if ( datilelist != null && datilelist.size() > 0) {
								String salebranchno = datilelist.get(0)
								.getSaleBranchNo();
								releaseAlarm(datilelist, redemporder,
										issueCompanyNo, alarmRule, tranDate,
										salebranchno);
							}
						}
					}
				}
			}
		} catch (Exception e) {
		}
	}

	/**
	 * 发布风险信息
	 * 
	 * @param tranDate
	 * @param alarmRule
	 * @param issueCompany
	 * 
	 * @param alarmOrders存在风险的销售单
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(List<RedempOrder> datilelist,
			Object[] redemporder, String issueCompanyNo,
			AlarmRule alarmRule, Date tranDate, String salebranchno)
			throws PCPSApplicationException {
		AlarmInfo alarmInfo = new AlarmInfo();
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(salebranchno);
		alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
		alarmInfo.setIssueCompanyNo(issueCompanyNo);
		alarmInfo.setBranchNo(salebranchno);
		alarmInfo.setBranchName(saleBranch.getSaleBranchName());
		alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
		if (redemporder[1] != null) {
			PersonalCustom proxyPersonalCustom = personalCustomDaoImpl
					.getPersonalCustomByPersonalId(new BigInteger(redemporder[1].toString()));
			alarmInfo.setRiskCustomName(proxyPersonalCustom.getName());
		} else if (redemporder[2] != null) {
			OrganizeCustom organizeCustom = organizeCustomDaoImpl
					.getOrganizeCustomByOrganizeId(new BigInteger(redemporder[2].toString()));
			alarmInfo.setRiskCustomName(organizeCustom.getFullName());
		}
		alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
		alarmInfo.setDealStatus("0");
		BigInteger alarmInfoId = alarmInfoDaoImpl.getMaxAlarmInfoId();
		alarmInfo.setAlarmInfoId(alarmInfoId);
		// 插入预警信息
		alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);
		for (int i = 0; i <= datilelist.size(); i++) {
			RedempOrder order = datilelist.get(i);
			AlarmInfoDetail alarmInfoDetail = new AlarmInfoDetail();
			alarmInfoDetail.setAlarmInfoId(alarmInfoId);
			alarmInfoDetail.setSeqNo(i);
			alarmInfoDetail.setBusinessType("2");
			alarmInfoDetail.setBusinessOrderId(order.getRedempOrderId());
			alarmInfoDetail.setTranDate(tranDate);
			alarmInfoDetailDaoImpl.insertAlarmInfoDetail(alarmInfoDetail);
		}
	}

	/**
	 * 时间减天数得到时间
	 * 
	 * @param time
	 * @param daynum
	 * @return
	 */
	public static String getSubductionDays(Date time, int daynum) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			Date beginDate = dft.parse(formatter.format(time));
			Calendar date = Calendar.getInstance();
			date.setTime(beginDate);
			date.set(Calendar.DATE, date.get(Calendar.DATE) - daynum);
			return dft.format(date.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date stringTodate(String in_str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date out_date = new Date();
		try {
			out_date = sdf.parse(in_str);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return out_date;
	}
}
